package com.asia5b.a5mall.member.fragment

import androidx.recyclerview.widget.LinearLayoutManager
import android.view.Gravity
import android.view.ViewGroup
import android.view.WindowManager
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.asia5b.a5bmall.resource.ActCons
import com.asia5b.a5mall.member.adapter.PhoneCodeAdapter
import com.asia5b.a5mall.member.databinding.DialogSelectcodeBinding
import com.asia5b.a5mall.middleware.base.SimpleDialogFragment
import com.asia5b.a5mall.middleware.logic.viewmodel.member.SelectPhoneCodeDFVM
import com.asia5b.a5mall.middleware.model.member.CountryModel
import kotlinx.android.synthetic.main.dialog_selectcode.*

/**
 * 选择手机区号
 */
@Route(path = ActCons.SelectCountryPhoneCodeDF)
class SelectCountryPhoneCodeDF : SimpleDialogFragment<SelectPhoneCodeDFVM, DialogSelectcodeBinding>() {
    override val viewModelClazz: Class<SelectPhoneCodeDFVM>
        get() = SelectPhoneCodeDFVM::class.java

    override fun getDataBinding(container: ViewGroup?): DialogSelectcodeBinding {
        return DialogSelectcodeBinding.inflate(layoutInflater,container,false)
    }

    private lateinit var phoneCodeAdapter: PhoneCodeAdapter
    private var selectCountryModel: MutableList<CountryModel>? = null

    override fun initView() {
        mViewModel.loadPhoneCodeList()
        rv_code.layoutManager = LinearLayoutManager(context)
        phoneCodeAdapter = PhoneCodeAdapter(mutableListOf())
        rv_code.adapter = phoneCodeAdapter
        phoneCodeAdapter.setOnItemClickListener { _, _, position ->
            selectCountryModel?.let {
//                callBack?.callback(it[position])
                mSureClick?.invoke(it[position])
                dismiss()
            }
        }
    }

    override fun initData() {
    }

    override fun initEvent() {
        //空白区域
        rl_code_close.setOnClickListener { dismiss() }
    }

 override   fun startObserver()
    {
        mViewModel.loadPhoneCodeListLiveData.observe(this, Observer {
            renderRegion(it)
        })
    }


    override val gravity: Int
        get() = Gravity.BOTTOM
    override val widthStyle: Int
        get() = WindowManager.LayoutParams.MATCH_PARENT

    override val heightPercent: Double
        get() = 0.7

    fun renderRegion(data: MutableList<CountryModel>) {
        selectCountryModel = data
        phoneCodeAdapter.setNewData(selectCountryModel)
//        selectCountryModel?.let {
//            callBack?.callback(it[0])
//        }
    }
}